package ch14;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Ballmove extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane mainpane = new BorderPane();
        Pane circlepane = new Pane();
        Circle circle = new Circle(30, 30, 30);
        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);
        circlepane.getChildren().add(circle);
        mainpane.setCenter(circlepane);
        GridPane buttons = new GridPane();
        Button up = new Button("上");
        Button down = new Button("下");
        Button left = new Button("左");
        Button right = new Button("右");
        buttons.add(up, 0, 0);
        buttons.add(down, 1, 0);
        buttons.add(left, 2, 0);
        buttons.add(right, 3, 0);
        mainpane.setBottom(buttons);
        Scene scene = new Scene(mainpane, 100, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
        up.setOnMousePressed(e -> {
            circle.setCenterY(circle.getCenterY() - 30 > 0 ? circle.getCenterY() - 15 : circle.getCenterY());
        });
        down.setOnMousePressed(e -> {
            circle.setCenterY(
                    circle.getCenterY() + 30 < scene.getHeight() ? circle.getCenterY() + 15 : circle.getCenterY());
        });
        left.setOnMousePressed(e -> {
            circle.setCenterX(circle.getCenterX() - 30 > 0 ? circle.getCenterX() - 15 : circle.getCenterX());
        });
        right.setOnMousePressed(e -> {
            circle.setCenterX(
                    circle.getCenterX() + 30 < scene.getWidth() ? circle.getCenterX() + 15 : circle.getCenterX());
        });
    }
}